package com.dtsz.m2.ui.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Request;
import com.bumptech.glide.Glide;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.adapter.MeadWallAdatper;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.inter.OnGetInputFinish;
import com.dtsz.m2.ui.model.MwRecordVo;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class MedalWallActivity extends BaseActivity implements OnGetInputFinish {
    View btnClick;
    private ImageView[] images = new ImageView[4];
    private ImageView[] imageruise = new ImageView[4];//巡航者
    private ImageView[] messengers = new ImageView[4];//使者
    private ImageView[] alliances = new ImageView[1];//终极


    private TextView tvAccount, tvNum;

    private ListView lvMadeWall;
    private MeadWallAdatper meadWallAdatper;
    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_medal_wall);
        super.onCreate(savedInstanceState);
        initData();

    }

    @Override
    public void initView() {
        super.initView();
        btnClick = findViewById(R.id.btn_click);
        images[0] = findViewById(R.id.iv_open1);
        images[1] = findViewById(R.id.iv_open2);
        images[2] = findViewById(R.id.iv_open3);
        images[3] = findViewById(R.id.iv_open4);
        imageruise[0] = findViewById(R.id.iv_cruise1);
        imageruise[1] = findViewById(R.id.iv_cruise2);
        imageruise[2] = findViewById(R.id.iv_cruise3);
        imageruise[3] = findViewById(R.id.iv_cruise4);

        messengers[0] = findViewById(R.id.iv_messenger1);
        messengers[1] = findViewById(R.id.iv_messenger2);
        messengers[2] = findViewById(R.id.iv_messenger3);
        messengers[3] = findViewById(R.id.iv_messenger4);
        alliances[0] = findViewById(R.id.iv_alliance);

        tvAccount = findViewById(R.id.tv_account);
        tvNum = findViewById(R.id.tv_num);
    }

    @Override
    public void initClick() {
        super.initClick();
        btnClick.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_click:

                flag = true;
                getMedalTask();
                break;
        }
    }

    void initData() {
        loading();
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.SELECTLISTHOME_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    void getMedalTask() {
        loading();
        hucRequestInterfase.executeStr(this, MwRecordVo.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.GETMEDALTASK_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    void updateStatus(MwRecordVo mwRecordVo) {
        loading();
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_3, Request.Method.POST, UrlConfig.UPDATESTATUS_URL,
                hucParameInterfase.getRequestParameter(mwRecordVo), false);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                String responseStr = (String) response;
                try {
                    JSONObject json = new JSONObject(responseStr);
                    String cruise = json.getString("cruise");
                    String count = json.getString("count");
                    String alliance = json.getString("alliance");
                    String mail = json.getString("mail");
                    String messenger = json.getString("messenger");
                    String openUp = json.getString("openUp");
                    if (StringUtils.isEmpty(mail) || "null".equals(mail)) {
                        tvAccount.setText("账号：未设置");
                    } else {
                        tvAccount.setText("账号：" + mail);
                    }
                    tvNum.setText(count);
                    getString(images, openUp);
                    getString(imageruise, cruise);
                    getString(messengers, messenger);
                    getString(alliances, alliance);
                    System.out.println(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                List<MwRecordVo> mwRecordVoList = (List<MwRecordVo>) response;
                meadWallAdatper = new MeadWallAdatper(this, mwRecordVoList, R.layout.item_meadl, this);
                if (flag = true) {
                    uploadHeadImage();
                }
                lvMadeWall.setAdapter(meadWallAdatper);
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                getMedalTask();
                flag = false;
                showToast(response.toString());
                popupWindow.dismiss();
                break;
        }
        loadDismiss();
    }

    @Override
    public void getInputFinish(MwRecordVo mwRecordVo) {
        mwRecordVo.setToken(pageVo.getToken());
        mwRecordVo.setLanguage("1");
        updateStatus(mwRecordVo);
    }

    /**
     * 上传头像
     */
     PopupWindow popupWindow;
    private void uploadHeadImage() {
        View view = LayoutInflater.from(this).inflate(R.layout.popupwindow_meadl, null);
        lvMadeWall = view.findViewById(R.id.lv_made_wall);popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        View parent = LayoutInflater.from(this).inflate(R.layout.activity_identification, null);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        view.findViewById(R.id.ll_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                flag = true;
                initData();
            }
        });
        //popupWindow在弹窗的时候背景半透明
        final WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.5f;
        getWindow().setAttributes(params);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                params.alpha = 1.0f;
                getWindow().setAttributes(params);

            }
        });
    }

    /**
     * json 字符串装换数组
     *
     * @param strs
     * @return
     * @throws JSONException
     */
    private void getString(ImageView[] imageViews, String strs) throws JSONException {
        JSONArray jsonArray = new JSONArray(strs);
        for (int j = 0; j < jsonArray.length(); j++) {
            Glide.with(this).load(jsonArray.getString(j)).into(imageViews[j]);
        }

    }


}
